define(function (require, exports, module) {
    exports.ready = function () {
        require.async('$', function () {
            $(function () {
                $('.load').hide();
                FastClick.attach(document.body);
                $('.classify').click(function () {
                    $('.menu-lists').addClass('show-menu');
                });
                $('.close').click(function () {
                    $('.menu-lists').removeClass('show-menu');
                });

                $("#pay_type").on("click", ".weipay", function () {
                    $(this).removeClass('btn-gray').addClass('weipay_check').siblings().removeClass('weipay_check').addClass('btn-gray');
                });

                $('.order-all').click(function () {
                    $('.order-type').addClass('show-order');
                });
                $('.close').click(function () {
                    $('.order-type').removeClass('show-order');
                });

                $('#pj-close').click(function () {
                    $('#order-comment').hide();
                });

                $('.pinjia').click(function () {
                    $('.pop-box').css('display', 'block');
                    var val = $(this).data('id');
                    var order_id = $(this).data('order_id');
                    $('.rstid').val(val);
                    $('.order_id').val(order_id);
                });

                $('.star').click(function () {
                    $(this).removeClass('grey-star');
                    $(this).prevAll().removeClass('grey-star');
                    $(this).nextAll().addClass('grey-star');
                });

                //评价订单
                $(document).on('click', '.pj-btn', function () {
                    var score1 = $('.star1').find(".grey-star").length;
                    var score2 = $('.star2').find(".grey-star").length;
                    var score3 = $('.star3').find(".grey-star").length;
                    var id = $(".rstid").val();
                    var comment = $('#comment').val();
                    var order_id = $(".order_id").val();
                    var res_id = $('#res_id').val();
                    if (score1 == 5) {
                        $.alert('error', '请评价口味');
                        return false;
                    }

                    if (score2 == 5) {
                        $.alert('error', '请评价质量');
                        return false;
                    }
                    if (score3 == 5) {
                        $.alert('error', '请评价配送');
                        return false;
                    }

                    var data = {};
                    var json = {};
                    data.score = 5 - score1;
                    data.score_send = 5 - score3;
                    data.score_zl = 5 - score2;
                    data.comment = comment;
                    data.id = id;
                    data.order_id = order_id;
                    json.data = data;
                    json.url = '/restaurantstyy/ZduMobile/comment?res_id=' + res_id;
                    json.method = 'post';
                    json.type = 'json';
                    $.getAjax(json, true, reload, '');
                    $('.pop-box').css('display', 'none');
                });

                //取消订单
                $(document).on('click', '.reset', function () {
                    var order_id = $(this).data('order_id');
                    var res_id = $('#res_id').val();
                    $.confirm("确定取消该订单吗？", drop);

                    function drop() {
                        var data = {};
                        var json = {};
                        data.order_id = order_id;
                        json.data = data;
                        json.url = '/restaurantstyy/ZduMobile/resetOrder?res_id=' + res_id;
                        json.method = 'post';
                        json.type = 'json';
                        $.getAjax(json, true, reload, '');
                    }
                });

                //确认收货
                $(document).on('click', '.sure-btn', function () {
                    var order_id = $(this).data('order_id');
                    var res_id = $('#res_id').val();
                    $.confirm("确认收货吗？", drop);

                    function drop() {
                        var data = {};
                        var json = {};
                        data.order_id = order_id;
                        json.data = data;
                        json.url = '/restaurantstyy/ZduMobile/sureOrder?res_id=' + res_id;
                        json.method = 'post';
                        json.type = 'json';
                        $.getAjax(json, true, reload, '');
                    }
                });

                //提交订单
                var res_id = $('#res_id').val();
                $(document).on('click', '.pay-btn', function () {
                    var username = $('.username').val();
                    var mobile = $('.mobile').val();

                    var restaurant_id = $('.restaurant_id').val();
                    var memo = $('#memo').val();
                    var order_id = $(this).data('order_id');


                    var info = {};
                    var json = {};
                    info.order_id = order_id;
                    info.memo = memo;
                    info.restaurant_id = restaurant_id;
                    info.pay_type = 4;
                    fm.confirm('<p class="font-16">提示</p ><p class="font-gray9 margint-10">确定支付？</p >', function (callback) {
                        if (callback == 1) {
                            fm.closeLayer();
                            storedPay(info);
                        } else {

                        }
                    }, 'border-top: 4px solid #00a5f3;min-width: 64%;')

                });

                //支付接口
                function storedPay(info) {

                    $.ajax({
                        'url': "/restaurantstyy/ZduMobile/orderPay?res_id=" + res_id,
                        'dataType': 'json',
                        'type': 'POST',
                        'data': info,
                        'async': false,
                        success: function (data) {
                            if (data.status == 'WS') {
                                var wxpay = $.parseJSON(data.url);
                                pay(wxpay);
                            } else if (data.status == 'BS') {
                                fm.alert('付款成功');
                                window.sessionStorage.setItem("local_url", "/restaurantstyy/ZduMobile/myOrder/res_id/" + res_id);
                                setTimeout(redirect, 2000);
                            } else if (data.status == 'CampusCard') { //校园卡支付
                                fm.alert('付款成功');
                                window.sessionStorage.setItem("local_url", "/restaurantstyy/ZduMobile/myOrder/res_id/" + res_id);
                                setTimeout(redirect, 2000);
                            } else {
                                fm.alert(data.message);
                                return false;
                            }
                        }
                    });
                }


                //微支付
                function pay(wxpay) {
                    var res_id = $('#res_id').val();
                    if (typeof WeixinJSBridge == "undefined") {
                        if (document.addEventListener) {
                            document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                        } else if (document.attachEvent) {
                            document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                            document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                        }
                    } else {
                        WeixinJSBridge.invoke(
                            'getBrandWCPayRequest',
                            wxpay,
                            function (res) {
                                WeixinJSBridge.log(res.err_msg);
                                if (res.err_msg == "get_brand_wcpay_request:ok") {
                                    paySuccess();
                                } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                                    $.alert('error', '付款已取消');
                                    setTimeout(location.href = "/restaurantstyy/ZduMobile/myorder/res_id/" + res_id, 2000);
                                } else {
                                    $.alert('error', '调用微支付失败');
                                    return false;
                                }
                            }
                        );
                    }
                }

                function paySuccess() {
                    var res_id = $('#res_id').val();
                    location.href = "/restaurantstyy/ZduMobile/myOrder/res_id/" + res_id;
                }

                function redirect() {
                    location.href = window.sessionStorage.getItem("local_url");
                    window.sessionStorage.setItem("local_url", null);
                }

                //页面刷新
                function reload() {
                    setTimeout('location.reload()', 2000);
                }
            });
        });
    };
});